#include<iostream>
#include<string>
#include<unistd.h>
#include"inc/HttpServer.h"
using namespace std;
using namespace http_server;

#define default_port 8821
#define default_ip "0"
#define DEFAULTPATH "./opt" 

void run(size_t port, string ip)
{
    int ret = chdir(DEFAULTPATH);
    if(-1 == ret)
        perror("chdir err");
    if(ip == "0")
    {
        HttpServer server(port);
        server.start();
    }
    else 
    {
        HttpServer server(ip, port);
        server.start();
    }
}

int main(int argc, char* argv[])
{
    size_t port;
    string ip;
    if(argv[1] == NULL)
    {
        port = default_port;
        ip = default_ip;
    }
    else if(argv[2] == NULL)
    {
        port = atoi(argv[1]);
        ip = default_ip;
    }
    else
    {
        port = atoi(argv[1]);
        ip = argv[2];
    }

    run(port, ip);

    return 0;
}